package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.FacilityInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private AttrValueMapper attrValueMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private PaymentTypeMapper paymentTypeMapper;
    @Resource
    private FeeValueMapper feeValueMapper;
    @Resource
    private LeaseTermMapper leaseTermMapper;
    @Resource
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    //分页查询房间列表
    @Override
    public IPage<RoomItemVo> findRoomItemVoPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.findRoomItemVoPage(page,queryVo);
    }
    //根据id获取详情信息
    @Override
    public RoomDetailVo findRoomDetailVoByRoomId(Long id) {
        //1.获取RoomInfo
        //这里不能用mybatis-plus自带的getById()，因为我们要返回全部的房间信息，包括被删除的(is_deleted = 1)！！！所以要自己写一个sql
        RoomInfo roomInfo = roomInfoMapper.selectAllById(id);
        //1.1 若不存在该房间，则直接返回空值,在这里打断就行了，因为后面的没必要执行
        if (roomInfo == null){
            return null;
        }
        //2.获取【ApartmentItemVo】传入房间对象对应的公寓的id
        ApartmentItemVo apartmentItemVo = apartmentInfoService.findApartmentItemVoById(roomInfo.getApartmentId());
        //3.获取graphVoList
        List<GraphVo> graphVoList = graphInfoMapper.findListByItemTypeAndId(ItemType.ROOM,id);
        //4.获取attrValueVoList
        List<AttrValueVo> attrValueVoList = attrValueMapper.findListByRoomId(id);
        //5.获取facilityInfoList
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findListByRoomId(id);
        //6.获取labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.findListByRoomId(id);
        //7.获取paymentTypeList
        List<PaymentType> paymentTypeList = paymentTypeMapper.findPaymentTypeByRoomId(id);
        //8.获取feeValueVoList
        List<FeeValueVo> feeValueVoList = feeValueMapper.findListByRoomId(id);
        //9.获取leaseTermList
        List<LeaseTerm> leaseTermList = leaseTermMapper.findListByRoomId(id);
        //10.确定房间入住状态
        QueryWrapper<LeaseAgreement> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(LeaseAgreement::getRoomId,id)
                .in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING);
        //若count > 0 ,那么就说明房间已经入住了
        Long count = leaseAgreementMapper.selectCount(queryWrapper);
        //11.接下来他妈开始铠甲合体，写了这么多，妈的
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        roomDetailVo.setIsCheckIn(count > 0);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        //12.如果完成了此处操作，即根据id获取房间的详情信息
        // 那么就说明用户在前端页面里点开了这个房间页面
        // 那么我们就在此处保存用户的浏览记录，作为浏览历史，加载到数据库里面
        // 传入两个参数：1.当前登录用户的id  2.房间的id
        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(),id);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> findRoomItemVoPageByApartmentId(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.findRoomItemVoPageByApartmentId(page,id);
    }


}




